import Vue from 'vue'
import VueRouter from 'vue-router'

import index from '../views/index.vue'
import detail from '../views/detail.vue'
import login from '../views/login.vue'
import my from '../views/my.vue'
import collectionPage from '../views/collectionPage.vue'

Vue.use(VueRouter)
const router = new VueRouter({
	routes: [{
			path: '/',
			component: index,
			meta:{
					login:false
				},
		},
		{
			path: '/detail',
			component: detail,
			meta:{
					login:false
				},
		},
		{
			path: '/login',
			component: login,
			meta:{
					login:false
				},
		},
		{
			path: '/my',
			component: my,
			meta:{
					login:true
				},
		},
		{
			path: '/collectionPage',
			component: collectionPage,
			meta:{
					login:false
				},
		},
	]
})

router.beforeEach((to, from, next) => {
	if(to.meta.login){
		let userinfo = JSON.parse(localStorage.getItem('userinfo'))
		// console.log(userinfo)
		if(userinfo==null||userinfo==undefined||!userinfo.islogin){
			router.push({
				path:'/login',
				// query:{
				// 	fullPath:to.fullPath
				// }
			})
		}else{
			next()
		}
	}else{
		next()
	}
})
export default router